﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Oracle.ManagedDataAccess.Client;

namespace WFL.RBAC.Oracle
{
    public class OperationDAL : BaseDAL, IOperation
    {

        #region 接口方法

        public void Create(Operation item)
        {
            OnCreate(item);
        }

        public void Delete(int resourceId, int id)
        {
            OnDelete(resourceId, id);
        }

        public void Update(Operation item)
        {
            OnUpdate(item);
        }

        public IList<Operation> GetList(int resourceId)
        {
            return OnGetList(resourceId);
        }

        public Operation Get(int resourceId, int id)
        {
            return OnGet(resourceId, id);
        }

        public Operation Get(int resourceId, string name)
        {
            return OnGet(resourceId, name);
        }

        #endregion

        #region 虚方法

        protected virtual void OnCreate(Operation item)
        {
            Orm.Insert<Operation>(item);
        }

        protected virtual void OnDelete(int resourceId, int id)
        {
            string condition = "resourceId=:ResourceId and id=:Id";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("ResourceId",resourceId),
                new OracleParameter("Id",id),
            };
            Orm.Delete<Operation>(condition, parameters);
        }

        protected virtual void OnUpdate(Operation item)
        {
            string condition = "resourceId=:ResourceId and id=:Id";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("ResourceId",item.ResourceId),
                new OracleParameter("Id",item.Id),
            };
            Orm.Update<Operation>(item, null, null, condition, parameters);
        }

        protected virtual IList<Operation> OnGetList(int resourceId)
        {
            string condition = "resourceId=:ResourceId";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("ResourceId",resourceId),
            };
            return Orm.SelectList<Operation>(condition, parameters, null);
        }

        protected virtual Operation OnGet(int resourceId, int id)
        {
            string condition = "resourceId=:ResourceId and id=:Id";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("ResourceId",resourceId),
                new OracleParameter("Id",id),
            };
            IList<Operation> list = Orm.SelectList<Operation>(condition, parameters, null);
            if (list == null && list.Count == 0)
            {
                return null;
            }
            return list[0];
        }

        protected virtual Operation OnGet(int resourceId, string name)
        {
            string condition = "resourceId=:ResourceId and name=:Name";
            OracleParameter[] parameters = new OracleParameter[]
            {
                new OracleParameter("ResourceId",resourceId),
                new OracleParameter("Name",name),
            };
            IList<Operation> list = Orm.SelectList<Operation>(condition, parameters, null);
            if (list == null && list.Count == 0)
            {
                return null;
            }
            return list[0];
        }

        #endregion

    }
}
